import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        while (!que.isEmpty()){
            int size = que.size();
            List<Integer> level = new ArrayList<>();
            while (size != 0){
                TreeNode cur = que.poll();
                level.add(cur.val);
                if (cur.left != null){
                    que.offer(cur.left);
                }
                if (cur.right != null){
                    que.offer(cur.right);
                }
                size--;
            }
            ret.add(level);

        }
        return ret;


    }
}
public class untitled1 {
}
